package com.qiyigu.itc.speech.baidu;

import android.os.Message;

import com.qiyigu.itc.speech.service.BaseServiceImpl;
import com.qiyigu.itc.speech.util.Constant;


/**
 * <p>Author: Leo xu</p>
 * <p>Date: 2017/12/7 11:08</p>
 * <p>Desc: 语音 解析器
 */
public class BaiduSpeechParserServiceImpl extends BaseServiceImpl {

    /**
     * appName = 微信,爱奇艺,百度音乐,文件管理,斗鱼直播,酷狗音乐,乐视视频,聚力视频,QQ音乐,搜狐视频,腾讯视频,天天动听
     * appHandle = 打开
     * changeNum= 一,二,三,四,五,六,七,八,九,十,十一,十二,十三,十四,十五,十六,十七,十八,加,减
     * controllerControl = 开始,暂停,继续,停止,加速,减速,加坡,减坡
     * controllerElement = 速度,坡度,音量
     * hardwareName = 风扇
     * controllerSwitch = 打开,关闭
     */

    private static String[] controllerControls = {"开始", "暂停", "继续", "停止", "加速", "减速", "加坡", "减坡"};
    private static String[] controllerElements = {"速度", "坡度", "音量", "风扇"};
    private static String[] controllerSwitchs = {"打开", "关闭", "加", "减"};
    private static String[] changeNums = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一",
            "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "二十一", "二十二", "二十三", "二十四"};
    private static String[] changeNum2s = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
            "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"};

    protected static BaiduSpeechParserServiceImpl service = null;

    protected BaiduSpeechParserServiceImpl() {
        service = this;
    }

    public static synchronized BaiduSpeechParserServiceImpl getInstance() {
        if (service == null) {
            service = new BaiduSpeechParserServiceImpl();
        }
        return service;
    }

    public Message parserSpeechStr(String strResult) {
        Message msg = parserControllerCmd(strResult);
        if (msg != null) {
            return msg;
        }

        msg = parserControllerElements(strResult);
        if (msg != null) {
            return msg;
        }

        msg = parserControllerSwitchs(strResult);
        if (msg != null) {
            return msg;
        }

        return null;
    }

    /***
     * 判断 控制的命令
     * @param strResult
     * @return
     */
    private Message parserControllerCmd(String strResult) {
        for (int i = 0; i < controllerControls.length; i++) {
            //判断是否为控制命令
            if (strResult.contains(controllerControls[i])) {
                Message msg = new Message();
                switch (i) {
                    case 0:
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_START;
                        break;
                    case 1:
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_PAUSE;
                        break;
                    case 2:
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_RESTART;
                        break;
                    case 3:
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_STOP;
                        break;
                    case 4:
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SPEED_ADD;
                        break;
                    case 5:
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SPEED_REDUCE;
                        break;
                    case 6:
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SLOPE_ADD;
                        break;
                    case 7:
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SLOPE_REDUCE;
                        break;
                    default:
                        break;
                }
                return msg;
            }
        }
        return null;
    }

    private Message parserControllerElements(String strResult) {
        for (int i = 0; i < controllerElements.length; i++) {
            //判断是否 有相应 的控制元素{"速度", "坡度", "音量", "风扇"};
            if (strResult.contains(controllerElements[i])) {
                Message msg = new Message();
                switch (i) {
                    case 0:
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SPEED;
                        Integer speedInt = parserChangeNum(strResult);
                        if (speedInt != null) {
                            msg.arg1 = speedInt.intValue();
                            return msg;
                        }
                        speedInt = parserControllerSwitch(strResult);
                        if (speedInt != null) {
                            if (speedInt.intValue() == 3) {
                                msg.what = Constant.SpeechParserConstant
                                        .SPEECH_CONRTOLLER_SPEED_ADD;
                            } else if (speedInt.intValue() == 4) {
                                msg.what = Constant.SpeechParserConstant
                                        .SPEECH_CONRTOLLER_SPEED_REDUCE;
                            }
                        }
                        break;
                    case 1:
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_SLOPE;
                        Integer slopeInt = parserChangeNum(strResult);
                        if (slopeInt != null) {
                            msg.arg1 = slopeInt.intValue();
                            return msg;
                        }

                        slopeInt = parserControllerSwitch(strResult);
                        if (slopeInt != null) {
                            if (slopeInt.intValue() == 3) {
                                msg.what = Constant.SpeechParserConstant
                                        .SPEECH_CONRTOLLER_SLOPE_ADD;
                            } else if (slopeInt.intValue() == 4) {
                                msg.what = Constant.SpeechParserConstant
                                        .SPEECH_CONRTOLLER_SLOPE_REDUCE;
                            }
                        }
                        break;
                    case 2:
                        msg.what = Constant.SpeechParserConstant.SPEECH_CONRTOLLER_VOICE;
                        Integer voiceInt = parserControllerSwitch(strResult);
                        if (voiceInt != null) {
                            if (voiceInt.intValue() == 1) {
                                msg.what = Constant.SpeechParserConstant
                                        .SPEECH_CONRTOLLER_VOICE_OPEAN;
                            } else if (voiceInt.intValue() == 2) {
                                msg.what = Constant.SpeechParserConstant
                                        .SPEECH_CONRTOLLER_VOICE_CLOSE;
                            } else if (voiceInt.intValue() == 3) {
                                msg.what = Constant.SpeechParserConstant
                                        .SPEECH_CONRTOLLER_VOICE_ADD;
                            } else if (voiceInt.intValue() == 4) {
                                msg.what = Constant.SpeechParserConstant
                                        .SPEECH_CONRTOLLER_VOICE_REDUCE;
                            }
                        }
                        break;
                    case 3:
                        Integer fanInt = parserControllerSwitch(strResult);
                        if (fanInt != null) {
                            if (fanInt.intValue() == 1) {
                                msg.what = Constant.SpeechParserConstant
                                        .SPEECH_CONRTOLLER_OPEAN_FAN;
                            } else if (fanInt.intValue() == 2) {
                                msg.what = Constant.SpeechParserConstant
                                        .SPEECH_CONRTOLLER_CLOSE_FAN;
                            }
                        }
                        break;

                    default:
                        break;
                }
                return msg;
            }
        }
        return null;
    }


    /***
     * 判断 控制的命令
     * @param strResult
     * @return
     */
    private Message parserControllerSwitchs(String strResult) {
        for (int i = 0; i < 2; i++) {
            //判断是否为控制命令
            if (strResult.contains(controllerSwitchs[i])) {
                Message msg = new Message();
                switch (i) {
                    case 0://打开
                        msg.what = Constant.SpeechParserConstant.SPEECH_APP_HANDLE_OPEN;
                        msg.obj = strResult;
                        break;

                    default:
                        break;
                }
                return msg;
            }
        }
        return null;
    }


    private Integer parserChangeNum(String strResult) {
        Integer numInt = null;
        //从最大值匹配
        for (int i = changeNums.length - 1; i > 0; i--) {
            //判断是否 有相应 的控制元素
            if (strResult.contains(changeNums[i])) {
                numInt = i + 1;
            }
        }
        if (numInt != null) {
            return numInt;
        }

        for (int i = changeNum2s.length - 1; i > 0; i--) {
            //判断是否 有相应 的控制元素
            if (strResult.contains(changeNum2s[i])) {
                numInt = i + 1;
            }
        }
        return numInt;
    }

    /**
     * @param strResult
     * @return 开1 关2 加3 减4
     */
    private Integer parserControllerSwitch(String strResult) {
        //判断是否 有相应 的控制元素 {"打开", "关闭"}
        if (strResult.contains(controllerSwitchs[0]) || strResult.contains
                ("开")) {
            return 1;
        } else if (strResult.contains(controllerSwitchs[1]) || strResult.contains
                ("关")) {
            return 2;
        } else if (strResult.contains(controllerSwitchs[2]) || strResult.contains
                ("增加")) {
            return 3;
        } else if (strResult.contains(controllerSwitchs[3]) || strResult.contains
                ("减少")) {
            return 4;
        }
        return null;
    }

}
